
#include "hpscclient.h"
#include <QSettings>
#include <iostream>
#include <QDataStream>
#include <QApplication>
#include "gssglobals.h"

#define DEBUG_ON    0
#define DEBUG if(DEBUG_ON)
//subversion
// - getCommand() deals with HPSCEvent of various types for the BUS handling
//

HPSCClient::HPSCClient(QObject *parent)
 : QTcpSocket(parent),
 m_hpsc(NULL)
{
	QSettings settings("../../gss.conf",QSettings::IniFormat);
	QString serialPort = settings.value("Serial_Ports/HPSC/port"/*,"/dev/ttyS0"*/).toString();
	if(!serialPort.isEmpty())
		m_hpsc = new CHPSC(serialPort,this);
	else std::cout<<"HPSCClient: could not find HPSC serial port setting in config file"<<std::endl;
	connect(this,SIGNAL(readyRead()),this,SLOT(getCommand()));
	connect(m_hpsc,SIGNAL(sig_hpscError(int,quint8)),this,SLOT(getHpscError(int,quint8)));
	//connect(m_hpsc,SIGNAL(sig_hpscReadout(int,float,float)),this,SLOT(hpscReadout(int,float,float)));
    connect(m_hpsc,SIGNAL(sig_hpscReadoutV(int, float)),this,SLOT(hpscReadoutV(int,float)));
    connect(m_hpsc,SIGNAL(sig_hpscReadoutA(int, float)),this,SLOT(hpscReadoutA(int,float)));
}


HPSCClient::~HPSCClient()
{
}




/*!
    \fn HPSCClient::getCommand()
 */
void HPSCClient::getCommand()
{
    DEBUG qDebug()<<"====================getCommand=======================";
    DEBUG qDebug()<<"====================getCommand=======================";
    DEBUG qDebug()<<"====================getCommand=======================";
    DEBUG qDebug()<<"====================getCommand=======================";
    DEBUG qDebug()<<"====================getCommand=======================";
    DEBUG qDebug()<<"====================getCommand=======================";
	int bytesInPipe = bytesAvailable();
    QPair<QString,long> strCmdWait;
	QDataStream in(this);
	HPSC_Command hpscCommand;
    bool serailConnect;
    quint32 type;
    quint8 padding;
    while(bytesInPipe >= sizeof(HPSC_Command)+sizeof(type))
    {
        in>>type;
        switch(type)
        {
            case HPSCEvent: in>>hpscCommand;
            ///testing
                    switch(hpscCommand.cmd )
                    {
                        case BUS1: DEBUG qDebug()<<"BUS1 BUS1 BUS1 BUS1 BUS1";
                            strCmdWait.first =  QString("VOLT1 ")+ QString::number(28);
                            strCmdWait.second = 15000;
                            QCoreApplication::postEvent(m_hpsc->eventLoop(),new HPSCEventBase(HPSCSTRVAL,strCmdWait));
                            strCmdWait.first =  QString("CURR1 ")+ QString::number(1);
                            strCmdWait.second = 15000;
                            QCoreApplication::postEvent(m_hpsc->eventLoop(),new HPSCEventBase(HPSCSTRVAL,strCmdWait));
                            bytesInPipe = bytesAvailable();
                            break;
                        case BUS2: DEBUG qDebug()<<"BUS2 BUS2 BUS2 BUS2 BUS2";
                            strCmdWait.first =  QString("VOLT2 ")+ QString::number(6);
                            strCmdWait.second = 15000;
                            QCoreApplication::postEvent(m_hpsc->eventLoop(),new HPSCEventBase(HPSCSTRVAL,strCmdWait));
                            strCmdWait.first =  QString("CURR2 ")+ QString::number(2.5);
                            strCmdWait.second = 15000;
                            QCoreApplication::postEvent(m_hpsc->eventLoop(),new HPSCEventBase(HPSCSTRVAL,strCmdWait));
                            strCmdWait.first =  QString("VOLT3 ")+ QString::number(6);
                            strCmdWait.second = 15000;
                            QCoreApplication::postEvent(m_hpsc->eventLoop(),new HPSCEventBase(HPSCSTRVAL,strCmdWait));
                            strCmdWait.first =  QString("CURR3 ")+ QString::number(2.5);
                            strCmdWait.second = 15000;
                            QCoreApplication::postEvent(m_hpsc->eventLoop(),new HPSCEventBase(HPSCSTRVAL,strCmdWait));
                            bytesInPipe = bytesAvailable();
                            break;
                        case BUS3:
                            DEBUG qDebug()<<"BUS3 BUS3 BUS3 BUS3 BUS3";
                            strCmdWait.first =  QString("VOLT2 ")+ QString::number(14);
                            strCmdWait.second = 15000;
                            QCoreApplication::postEvent(m_hpsc->eventLoop(),new HPSCEventBase(HPSCSTRVAL,strCmdWait));
                            strCmdWait.first =  QString("CURR2 ")+ QString::number(3);
                            strCmdWait.second = 15000;
                            QCoreApplication::postEvent(m_hpsc->eventLoop(),new HPSCEventBase(HPSCSTRVAL,strCmdWait));
                            strCmdWait.first =  QString("VOLT3 ")+ QString::number(14);
                            strCmdWait.second = 15000;
                            QCoreApplication::postEvent(m_hpsc->eventLoop(),new HPSCEventBase(HPSCSTRVAL,strCmdWait));
                            strCmdWait.first =  QString("CURR3 ")+ QString::number(3);
                            strCmdWait.second = 15000;
                            QCoreApplication::postEvent(m_hpsc->eventLoop(),new HPSCEventBase(HPSCSTRVAL,strCmdWait));
                            bytesInPipe = bytesAvailable();
                            break;
                        case BUS4:
                            //this will change to something dynamic, since the values will change accourding to but charge profile.
                            //this code its just for "demo" reasons.
                            strCmdWait.first =  QString("VOLT4 ")+ QString::number(28);
                            strCmdWait.second = 15000;
                            QCoreApplication::postEvent(m_hpsc->eventLoop(),new HPSCEventBase(HPSCSTRVAL,strCmdWait));
                            strCmdWait.first =  QString("CURR4 ")+ QString::number(2);
                            strCmdWait.second = 15000;
                            QCoreApplication::postEvent(m_hpsc->eventLoop(),new HPSCEventBase(HPSCSTRVAL,strCmdWait));
                            bytesInPipe = bytesAvailable();
                            break;
                        default: break;    
                    } 
            /////
//                         strCmdWait.first = HPSC_CommandToString(hpscCommand);
//                         strCmdWait.second = 30000;
//                         QCoreApplication::postEvent(m_hpsc->eventLoop(),new HPSCEventBase(HPSCSTRVAL,strCmdWait));
//                         bytesInPipe = bytesAvailable();
                break;
            case HPSCEventSerial: in>>serailConnect;
                             QCoreApplication::postEvent(m_hpsc->eventLoop(),new HPSCEventBase(HPSCEventSerial,serailConnect));
                                for(int i=0;i<sizeof(HPSC_Command)-sizeof(bool);++i) 
                                  in>>padding;
                break;
                default:  bytesInPipe = bytesAvailable();
                    break;
        }
    }
// 	while(bytesInPipe >= sizeof(HPSC_Command))
// 	{
// 		//DEBUG qDebug()<<"getting size:"<<bytesInPipe;
// 		in>>hpscCommand;
// 		//DEBUG qDebug()<<"command:"<<(int)hpscCommand.cmd<<" "<<hpscCommand.var;
// 		//TODO:
// 		//m_hpsc->sendCommand(hpscCommand);
// 		QPair<QString,long> strCmdWait(HPSC_CommandToString(hpscCommand),30000);
// 		QCoreApplication::postEvent(m_hpsc->eventLoop(),new HPSCEventBase(HPSCSTRVAL,strCmdWait));
// 		//QCoreApplication::
// 		bytesInPipe = bytesAvailable();
// 	}
}


/*!
    \fn HPSCClient::getHpscError()
 */
void HPSCClient::getHpscError(int src,quint8 err)
{
	
	QDataStream out(this);
	out<<MSG_ERR<<src<<err;
}


/*!
    \fn HPSCClient::sendError()
 */
void HPSCClient::sendError()
{
    /// @todo implement me
}


/*!
    \fn HPSCClient::hpscReadout(HPSC::SRC src,float volt,float amper)
 */
void HPSCClient::hpscReadout(int src,float volt,float amper)
{
	//DEBUG qDebug()<<"HPSCClient::hpscReadout";
	QDataStream out(this);
	out<<MSG_READOUT<<src<<volt<<amper;
	flush();
	//DEBUG qDebug()<<"=================================type:"<<(int)MSG_READOUT<<"src:"<<src<<" volt:"<<volt<<" amper:"<<amper;
}




/*!
    \fn HPSCClient::hpscReadoutA(int src, float amper)
 */
void HPSCClient::hpscReadoutA(int src, float amper)
{
    QDataStream out(this);
    DEBUG qDebug()<<"MSG_READOUT_A"<<src<<amper;
    out<<MSG_READOUT_A<<src<<amper;
    flush();
}


/*!
    \fn HPSCClient::hpscReadoutV(int src, float volt)
 */
void HPSCClient::hpscReadoutV(int src, float volt)
{
    QDataStream out(this);
    DEBUG qDebug()<<"MSG_READOUT_V"<<src<<volt;
    out<<MSG_READOUT_V<<src<<volt;
    flush();
}
